<?php

/**
 * This is the model class for table "{{att_select}}".
 *
 * The followings are the available columns in table '{{att_select}}':
 * @property integer $ztc_id
 * @property string $za_title
 * @property string $zpa_val
 * @property string $title_val
 */
class AttSelect extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return AttSelect the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{att_select}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('za_title, zpa_val', 'length', 'max'=>50),
			array('title_val', 'length', 'max'=>100),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('ztc_id, za_title, zpa_val, title_val', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'ztc_id' => 'Ztc',
			'za_title' => 'Za Title',
			'zpa_val' => 'Zpa Val',
			'title_val' => 'Title Val',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('ztc_id',$this->ztc_id);
		$criteria->compare('za_title',$this->za_title,true);
		$criteria->compare('zpa_val',$this->zpa_val,true);
		$criteria->compare('title_val',$this->title_val,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * 获取分类属性筛选标识
	 */
	public function getAttMark($catgoryId){
		$data = Yii::app()->filecache->get('att_mark');//获取缓存
		//存在缓存，返回缓存数据
		if(!empty($data)&&array_key_exists($catgoryId,$data)){
			return $data[$catgoryId];
		}
		
		$att = Attribute::model()->getSta($catgoryId,1,2);
		if(!empty($att)){
			foreach($att as &$val){
				$value = explode(',', $val['za_value']);
				foreach($value as $val2){
					$mark = $val['za_title']."_".$val2;
					//查询，如果不存在记录则新建记录
					$attmodel = $this->findByAttributes(array('title_val'=>$mark));
					if(empty($attmodel)){
						$attmodel = new AttSelect();
						$attmodel->za_title = $val['za_title'];
						$attmodel->zpa_val = $val2;
						$attmodel->title_val = $mark;
						$attmodel->save();
					}
					$val['mark'][] = $attmodel->attributes;
				}
			}
			//保存缓存数据
			$data[$catgoryId] = $att;
			Yii::app()->filecache->set('att_mark',$data,3600*24*30);
			return $data[$catgoryId];
		}
	}
	
	/**
	 * 获取标识对应的属性值对
	 */
	public function getMarkVal($id){
		//获取缓存
		$data = Yii::app()->filecache->get('mark_val');
		if(!empty($data)){
			foreach($id as $val){
				if(array_key_exists($val,$data)){
					$resutl[$val] = $data[$val];
				}
			}
			if(isset($resutl))
				return $resutl;
		}
		
		$attMark = $this->findAll();
		foreach($attMark as $val){
			$data[$val->ztc_id] = $val->attributes;
		}
		//保存缓存数据
		Yii::app()->filecache->set('mark_val',$data,3600*24*30);
		return $this->getMarkVal($id);
	}
	
	
}